﻿using ConfExpert.Utility;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.Rapor
{
    public partial class KumasSiparisFoyu : DevExpress.XtraReports.UI.XtraReport
    {

        DataSet ds;
        List<string[]> Captions = new List<string[]>();
        string[] CurrentCaptions
        {
            get
            {
                return Captions[LanguageIndex];
            }
        }

        public KumasSiparisFoyu()
        {
            InitializeComponent();

            ReportPrintTool printTool = new ReportPrintTool(this);
            printTool.PreviewForm.VisibleChanged += PreviewForm_VisibleChanged;
            Watermark.TextTransparency = 240;

            this.ShowPrintMarginsWarning = false;
        }



        void PreviewForm_VisibleChanged(object sender, EventArgs e)
        {
            DevExpress.XtraPrinting.Preview.PrintPreviewFormEx form = sender as DevExpress.XtraPrinting.Preview.PrintPreviewFormEx;

            BarButtonItem item = new BarButtonItem(form.PrintBarManager, "English");
            form.PrintBarManager.Bars[0].ItemLinks.Add(item, true);

            BarCheckItem mk = new BarCheckItem(form.PrintBarManager);
            mk.Caption = "Müşteri Kodları Dahil";
            form.PrintBarManager.Bars[0].ItemLinks.Add(mk, true);
            mk.CheckedChanged += mk_CheckedChanged;

            RepositoryItemSpinEdit riYukseklik = new RepositoryItemSpinEdit();
            riYukseklik.IsFloatValue = false;

            BarEditItem biYukseklik = new BarEditItem(form.PrintBarManager, riYukseklik);
            biYukseklik.Caption = "Satır Yüksekliği";
            biYukseklik.PaintStyle = BarItemPaintStyle.Caption;

            biYukseklik.EditValue = 25;
            biYukseklik.EditValueChanged += biYukseklik_EditValueChanged;

            form.PrintBarManager.Bars[0].ItemLinks.Add(biYukseklik, true);

            item.ItemClick += item_ItemClick;
        }

        bool MKIncluded = false;
        void mk_CheckedChanged(object sender, ItemClickEventArgs e)
        {
            MKIncluded = (sender as BarCheckItem).Checked;

            foreach (DataRow dr in ds.Tables[2].Rows)
            {
                string mk = "\n MK:" + dr["MusteriKodu"];
                if (MKIncluded)
                {
                    if (dr["MusteriKodu"].ToString() == "")
                        break;
                    else
                        dr["DesenNo"] = dr["DesenNo"] + mk;
                }
                else
                    dr["DesenNo"] = dr["DesenNo"].ToString().Replace(mk, "");
            }

            this.CreateDocument();
        }

        void biYukseklik_EditValueChanged(object sender, EventArgs e)
        {
            yukselik = Convert.ToSingle((sender as BarEditItem).EditValue) * 10;
            tblDetail.HeightF = yukselik;
            Detail.HeightF = yukselik;
            this.CreateDocument();
        }

        void item_ItemClick(object sender, ItemClickEventArgs e)
        {
            bool en = e.Item.Caption == "English";
            e.Item.Caption = en ? "Türkçe" : "English";
            if (en)
            {
                tcHeader.Text = "Purchase Order";
                xrTableCell11.Text = "Nominated Supplier";
                tc2.Text = "Issued By";
                xrTableCell1.Text = "Document Date";
                xrTableCell2.Text = "Notes";
                xrTableCell5.Text = "S. Del Date";
                xrTableCell9.Text = "F. Del Date";
                tc12.Text = "Article";
                tc13.Text = "Technical Specs.";
                tc14.Text = "Requests";
                tc15.Text = "Price";
                xrTableCell6.Text = "Notes";
                tc16.Text = "Sample";
                tc18.Text = "Total Qty";
                tc50.Text = "Order Date";
                tc51.Text = "Purchaser";
                tc52.Text = "Contact";
            }
            else
            {
                tcHeader.Text = "Kumaş Sipariş Föyü";
                xrTableCell11.Text = "Üretici Tercihi";
                tc2.Text = "Talebi Açan";
                xrTableCell1.Text = "Belge Tarihi";
                xrTableCell2.Text = "Notlar";
                xrTableCell5.Text = "Gömlek Termini";
                xrTableCell9.Text = "Kumaş Termin Notu";
                tc12.Text = "Artikel";
                tc13.Text = "Teknik Özellik";
                tc14.Text = "İstekler";
                tc15.Text = "Fiyat";
                xrTableCell6.Text = "Notlar";
                tc16.Text = "Numune";
                tc18.Text = "Toplam Metraj";
                tc50.Text = "Sipariş Tarihi";
                tc51.Text = "Siparişi Geçen";
                tc52.Text = "Siparişi Alan";
            }

            this.CreateDocument();
        }


        int LanguageIndex = 0;

        float yukselik = 250;



        public void KumasSiparisFoyuOlustur(DataSet data)
        {
            tcHeader.Text = "Kumaş Sipariş Föyü";

            ds = data;
            BindDstoReport();
            tblAyrim.Visible = false;
            tblSiparis.Visible = true;
            tblSiparis.TopF = tblAyrim.TopF;
            trRef.Visible = barcode.Visible = false;
            Watermark.Text = "";

        }

        public void KumasSiparisFoyuOnizlemeOlustur(DataSet data)
        {
            KumasSiparisFoyuOlustur(data);
            Watermark.Text = "Önizleme";
            //tcIlgili.Text = "Önizleme";
            //tcPONot.Text = "Önizleme";
            tcSipGecen.Text = AppSession.Lookups.V_User.FindByUserId(AppSession.MainForm.WhoAmI).Name;
            tcSipTarihi.Text = DateTime.Now.ToString("dd.MM.yyyy HH:mm");
        }

        public void TalepFoyuOlustur(DataSet data)
        {
            xrPageInfo1.RunningBand = GroupHeader1;
            tcHeader.Text = "Kumaş Talep Föyü";

            ds = data;

            trRef.Visible = barcode.Visible = true;
            Watermark.Text = "";
            BindDstoReport();
        }

        public void TalepFoyuOnizlemeOlustur(DataSet data)
        {
            TalepFoyuOlustur(data);
            Watermark.Text = "Önizleme";
            //tcPONot.Text = "Önizleme";
            tcRef.Text = "0";
        }

        void BindDstoReport()
        {
            barcode.ShowText = false;

            tblDetail.HeightF = yukselik;
            Detail.HeightF = yukselik;


            DataRow drPO = ds.Tables[0].Rows[0];

            tcPO.Text = drPO["PoNr"].ToString();

            tcPO1.Text = drPO["PoNr"].ToString();
           

            object qty = ds.Tables[3].Rows[0]["TotalQty"];
            if (qty == DBNull.Value)
                tcToplam.Text = "#";
            else
                tcToplam.Text = string.Format("{0:n0}", Convert.ToDecimal(qty)) + " mt.";

            tcTalebiAcan.Text = drPO["TalebiAcan"].ToString();
            tcPONot.Text = drPO["FoyNotu"].ToString();

            tcHeader.Text += " (" + drPO["TalepTipi"].ToString() + ")";

            tcBelgeTarihi.Text = drPO["BelgeTarihi"].ToString();

            tcUreticiTercihi.Text = drPO["Vendor"].ToString();

            trRef.Visible = barcode.Visible = drPO["POMpFoyId"].ToStr() != "";
            tcRef.Text = barcode.Text = drPO["POMpFoyId"].ToStr();


            tcIlgili.Text = drPO["Ilgili"].ToStr();

            tcSipGecen.Text = drPO["SipGecen"].ToStr();

            if (drPO["SiparisTarihi"] != DBNull.Value)
                tcSipTarihi.Text = ((DateTime)drPO["SiparisTarihi"]).ToString("dd.MM.yyyy HH:mm");
            else
                tcSipTarihi.Text = "";


            DataTable dtAyrim = ds.Tables[1];

            if (dtAyrim.Rows.Count > 0)
                foreach (DataRow drAyrim in dtAyrim.Rows)
                {
                    XRTableRow tr = new XRTableRow();

                    XRTableCell tc = new XRTableCell();

                    tc.Text = drAyrim["MRef"].ToString();
                    tc.Borders = ((DevExpress.XtraPrinting.BorderSide)((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Bottom)));
                    tc.WidthF = xrTableCell3.WidthF;
                    tc.Padding = new DevExpress.XtraPrinting.PaddingInfo(5, 5, 0, 0, 254F);
                    tr.Cells.Add(tc);

                    tc = new XRTableCell();

                    tc.Text = drAyrim["OrderRef"].ToString();
                    tc.Borders = ((DevExpress.XtraPrinting.BorderSide)((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Bottom)));
                    tc.Padding = new DevExpress.XtraPrinting.PaddingInfo(5, 5, 0, 0, 254F);
                    tc.WidthF = xrTableCell4.WidthF;
                    tr.Cells.Add(tc);

                    tc = new XRTableCell();

                    tc.Text = drAyrim["GomlekTermini"].ToString();
                    tc.Borders = ((DevExpress.XtraPrinting.BorderSide)((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Bottom)));
                    tc.Padding = new DevExpress.XtraPrinting.PaddingInfo(5, 5, 0, 0, 254F);
                    tc.WidthF = xrTableCell5.WidthF;
                    tr.Cells.Add(tc);

                    tc = new XRTableCell();

                    tc.Text = drAyrim["TerminNotu"].ToString();
                    tc.Borders = ((DevExpress.XtraPrinting.BorderSide)((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Bottom | DevExpress.XtraPrinting.BorderSide.Right)));
                    tc.Padding = new DevExpress.XtraPrinting.PaddingInfo(5, 5, 0, 0, 254F);
                    tc.WidthF = xrTableCell9.WidthF;

                    tr.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
                    tr.Cells.Add(tc);
                    tblAyrim.Rows.Add(tr);
                }
            else
                tblAyrim.Visible = false;


            DataTable dtDetail = ds.Tables[2];

            string DetailTableName = dtDetail.TableName;

            this.DataMember = DetailTableName;
            this.DataSource = ds;

            //tcDesenNo.DataBindings.Add("Text", null, DetailTableName + ".DesenNo");
            //tcDesenNo.BeforePrint += tcSatirli_BeforePrint;


            rtKumas.DataBindings.Add("Html", null, DetailTableName + ".DesenNo");
            rtKumas.BeforePrint += rtKumas_BeforePrint;

            DevExpress.XtraPrinting.PaddingInfo pad = new DevExpress.XtraPrinting.PaddingInfo(5, 5, 5, 2);

            rtKumas.Padding = pad;
            tcKalite.DataBindings.Add("Text", null, DetailTableName + ".Kalite");
            tcKalite.BeforePrint += tcSatirli_BeforePrint;

            tcMetre.DataBindings.Add("Text", null, DetailTableName + ".Istekler");
            tcMetre.BeforePrint += tcSatirli_BeforePrint;

            //tcFiyat.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
            tcFiyat.DataBindings.Add("Text", null, DetailTableName + ".Fiyat");
            tcFiyat.BeforePrint += tcSatirli_BeforePrint;

            tcPolNot.DataBindings.Add("Text", null, DetailTableName + ".PolNot");
            tcPolNot.BeforePrint += tcSatirli_BeforePrint;

            xrPhoto.DataBindings.Add("Image", null, DetailTableName + ".Photo");

            rtTeknikOzellik.Padding = pad;
            rtTeknikOzellik.BeforePrint += reTeknikOzellik;
            rtTeknikOzellik.DataBindings.Add("Html", null, DetailTableName + ".TeknikOzellik");
            rtTeknikOzellik.Font = tcKalite.Font;
        }

        void rtKumas_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            string text = rtKumas.Text;

            text = text.Replace(Environment.NewLine, "·");
            text = text.Replace(@"\n", "·");
            text = text.Replace("·", "<br>");

            //text = text.Replace(@"MK:", "<font color=red>MK:</font>");
            //text = text.Replace(@"ÜK:", "<font color=red>ÜK:</font>");
            //text = text.Replace(@"HL:", "<font color=red>HL:</font>");
            text = text.Replace(@"MK:", "<b>MK:</b>");
            text = text.Replace(@"ÜK:", "<b>ÜK:</b>");
            text = text.Replace(@"HL:", "<b>HL:</b>");
            rtKumas.Html = "<font face=calibri size=2>" + text + "</font>";
        }

        private void tcSatirli_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            DevExpress.XtraReports.UI.XRTableCell cell = (DevExpress.XtraReports.UI.XRTableCell)sender;
            cell.Multiline = true;
            cell.Text = cell.Text.Replace(@"\n", Environment.NewLine);
            //cell.Text = cell.Text.Replace("MK:", "<b>MK:</b>");
        }


        private void reTeknikOzellik(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            string text = rtTeknikOzellik.Text;

            text = text.Replace(Environment.NewLine, "·");
            text = text.Replace(@"\n", "·");
            text = text.Replace("·", "<br>");

            string[] pairs = text.Split(new string[] { "Talep:" }, StringSplitOptions.RemoveEmptyEntries);
            if (pairs.Length > 1)
                text = pairs[0] + "<font color=red><b><br>Talep:" + pairs[1] + "</b></font>";
            else if (pairs.Length == 1 && text.Contains("Talep:"))
                text = "<font color=red><b>Talep:" + pairs[0] + "</b></font>";


            rtTeknikOzellik.Html = "<font face=calibri size=2>" + text + "</font>";
        }

        private void tcPO1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (PrintingSystem.Document.PageCount == 0)
                e.Cancel = true;
        }

    }
}